package com.sen.controller;


import com.sen.common.api.ApiResult;
import com.sen.model.dto.CommentDTO;
import com.sen.model.entity.BmsComment;
import com.sen.model.entity.UmsUser;
import com.sen.model.vo.CommentVO;
import com.sen.service.BmsCommentService;
import com.sen.service.UmsUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import static com.sen.common.jwt.JwtUtil.USER_NAME;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author ergou
 * @since 2021-04-14
 */
@RestController
@RequestMapping("/comment")
public class BmsCommentController {
    @Resource
    private BmsCommentService bmsCommentService;

    @Resource
    private UmsUserService umsUserService;


    @GetMapping("/get_comments")
    public ApiResult<List<CommentVO>> getCommentsByTopicId(@RequestParam(value = "topicID",defaultValue = "1")String topicid ){
        List<CommentVO> lsBmsComment = bmsCommentService.getCommentsByTopicID(topicid);
        return ApiResult.success(lsBmsComment);
    }

    @PostMapping("/add_comment")
    public ApiResult<BmsComment> add_comment(@RequestHeader(value = USER_NAME) String userName,
                                             @RequestBody CommentDTO dto) {
        UmsUser user = umsUserService.getUserByUsername(userName);
        BmsComment comment = bmsCommentService.create(dto, user);
        return ApiResult.success(comment);
    }
}
